﻿CREATE FUNCTION [dbo].[PageCounts]
	(
		@LoanID int
	)
RETURNS TABLE
AS

RETURN ( 
SELECT t_ImageType.ID, t_ImageType.ID ImageTypeID, t_ImageType.ImageType, t_ImageType.TabIndex, Pages.[Count],@LoanID LoanID
FROM  (SELECT t_Image.ImageTypeID, COUNT(*) AS [Count]
               FROM   t_Image INNER JOIN
                              t_Page ON t_Image.ID = t_Page.ImageID
               WHERE (t_Image.LoanID = @LoanID)
               GROUP BY t_Image.ImageTypeID) Pages RIGHT OUTER JOIN
               t_ImageType ON Pages.ImageTypeID = t_ImageType.ID
 )



